//现在创建router的方式与vue2.x的版本已经很不同了
import {createRouter, createWebHashHistory} from 'vue-router';
import store from '../store';
const routes = [
    {
        path: '/',
        redirect: '/home',
    },
    {
        path: '/home',
        meta: {login: false,showTabBar: true},
        component: () => import('@/view/home/index.vue')
    },
    {
        path: '/shoppingCart',
        meta: {login: false,showTabBar: true},
        component: () => import('@/view/goods/shoppingCart.vue')
    },
      {
        path: '/sort',
        meta: {login: false,showTabBar: true},
        component: () => import('@/view/sort/index.vue')
    },
    {
        path: '/my',
        meta: {login: false,showTabBar: true},
        component: () => import('@/view/my/index.vue')
    },
    {
        path: '/login',
        meta: {login: false,showTabBar: false},
        component: () => import('@/view/login/login.vue')
    },
    {
        path: '/loginMobile',
        meta: {login: false,showTabBar: false},
        component: () => import('@/view/login/loginMobile.vue')
    }
];

const router = createRouter({
    history: createWebHashHistory(), //替代之前的mode，是必须的
    routes
});
router.beforeEach((to,from,next) => {

    store.commit('changeshowTabBar',to.meta.showTabBar);
    if (to.meta.login) {
        if (store.state.userInfo.token) {
            next();
        } else {
            next({
                path: '/loginMobile',
                query: {redirect: to.fullPath}
            })
        }
    } else {
        next();
    }
});
export default router;
